package com.whq.knowverywell.activity;

import android.app.NotificationManager;
import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.whq.knowverywell.R;
import com.whq.knowverywell.business.LoginUtil;
import com.whq.knowverywell.task.ChkAppUpdateTask;
import com.whq.knowverywell.update.AppUpdateUtil;
import com.whq.knowverywell.util.AppUtil;
import com.whq.knowverywell.util.LogUtil;
import com.whq.knowverywell.util.StringUtil;
import com.whq.knowverywell.util.constant.CfgConstant;
import com.whq.knowverywell.util.constant.ParamConstant;

/**
 * 关于页面
 * @author wanghuaiqiang
 *
 */
public class AboutActivity extends BaseActivity {
	private static final String TAG = AboutActivity.class.getSimpleName();
	
	private TextView mTvSendMail = null;
	
	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.about);
        
        TextView tvAppName = (TextView) findViewById(R.id.tv_app_name);
        tvAppName.setText(getString(R.string.app_name));
        
        TextView tvAppVersion = (TextView) findViewById(R.id.tv_app_version);
        tvAppVersion.setText(AppUtil.getVersionName(this));
        
        ImageView ivCheckUpdate = (ImageView) findViewById(R.id.iv_check_update);
        ivCheckUpdate.setOnClickListener(this);
        
        mTvSendMail = (TextView) findViewById(R.id.tv_send_mail);
        mTvSendMail.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);//下划线
        mTvSendMail.setOnClickListener(this);
    }
	
	@Override
	protected void onResume() {
		LogUtil.d(TAG, "onResume start ...");
		super.onResume();
		
		// 检测是否收到升级的参数
		Intent intent = getIntent();
		if (intent != null) {
			NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
			nm.cancel(CfgConstant.UPDATE_NOTIFY_ID);
			
			String appVersionName = intent.getStringExtra(ParamConstant.BUNDLE_KEY_APP_VERSION_NAME);
			String appApkName = intent.getStringExtra(ParamConstant.BUNDLE_KEY_APP_APK_NAME);
			String appIntro = intent.getStringExtra(ParamConstant.BUNDLE_KEY_APP_INTRO);
			if ( StringUtil.isNotBlank(appVersionName)
					&& StringUtil.isNotBlank(appApkName)
					) {
				AppUpdateUtil.doNewVersionUpdate(AboutActivity.this, appVersionName, appApkName, appIntro);
				return ;
			}
		}
	}
	
	@Override
    public void onClick(View v) {
        LogUtil.d(TAG, "onClick start ...");
        switch (v.getId()) {
        case R.id.iv_check_update:
        	new ChkAppUpdateTask(this, ChkAppUpdateTask.CHK_UPDATE_TYPE_MANUL, true).execute();
        	break;
        case R.id.tv_send_mail:
        	sendMail();
        	break;
        default:
            break;
        }
    }
	
	private void sendMail() {
		Intent intent = new Intent(android.content.Intent.ACTION_SEND);
        intent.setType("plain/text");
        String[] strEmailReciver = new String[]{mTvSendMail.getText().toString()};
        String strEmailBody = "公司名：" + LoginUtil.getLoginGroupCode(this) + "\r\n" +
        		"帐号：" + LoginUtil.getLoginUserName(this) + "\r\n" + 
        		"操作系统：android\r\n" + 
        		"操作系统版本：" + android.os.Build.VERSION.SDK + "\r\n" + 
        		"手机型号：" + android.os.Build.MODEL + "\r\n" + 
        		"app版本：" + AppUtil.getVersionName(this) + "\r\n" + 
        		"意见和建议：" + "\r\n"; 
        intent.putExtra(android.content.Intent.EXTRA_EMAIL, strEmailReciver); //设置收件人
        intent.putExtra(android.content.Intent.EXTRA_TEXT, strEmailBody); //设置内容
        startActivity(Intent.createChooser(intent, "有数反馈意见"));
	}
}
